﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using EasyToHelp.Business;
using EasyToHelp.Entity;
using EasyToHelp.Service;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace EasyToHelp.WEBSystem.Controllers
{
    public class AdminController : Controller
    {
        private readonly ISysUsersService users;
        public AdminController(ISysUsersService _users)
        {
            users = _users;
        }
        public IActionResult Index()
        {
            return View();
        }

        #region 登录模块
        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 登录操作
        /// </summary>
        /// <param name="uname"></param>
        /// <param name="upwd"></param>
        /// <returns></returns>
        public async Task<IActionResult> LoginDo(string uname, string upwd)
        {
            if (string.IsNullOrWhiteSpace(uname) || string.IsNullOrWhiteSpace(upwd))
            {
                return Json(new { status = 0, msg = "用户名或密码不能为空！" });
            }
            SysUsers user = new SysUsers() { UserName = uname, UserPwd = upwd };
            var model = users.GetUserList().Where(x=>x.UserName== uname&&x.UserPwd==upwd).FirstOrDefault();
            if (model == null)
            {
                return Json(new { status = 0, msg = "用户不存在，登录失败！" });
            }
            //把需要保存的用户数据转成一个字符串。
            var data = JsonConvert.SerializeObject(model);
            //用户标识
            var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
            identity.AddClaim(new Claim(ClaimTypes.Sid, model.Id.ToString()));
            identity.AddClaim(new Claim(ClaimTypes.Name, model.UserName));
            identity.AddClaim(new Claim(ClaimTypes.UserData, data));
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
            return Json(new { status = 1, msg = "登录成功！" });
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> LoginOut()
        {
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            //HttpContext.Response.Cookies.Delete("userinfo");
            //var h = HttpContext.Request.Path;
            return Redirect("/Admin/Login");
        }
        #endregion


        #region 系统用户管理模块
        public IActionResult SysUserlist()
        {
            var list = SysUsersBusiness.GetUsersList();
            return View(list);
        }
        #endregion



    }
}